/*
 * Copyright：fxsen <br/>
 * Date: 2022/1/27 21:30 <br/>
 * Email:fxsen@foxmail.com
 * Author: fxsen
 */
package com.fxsen.platform.web.controllers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fxsen.commons.core.annotation.RecordLog;
import com.fxsen.commons.core.annotation.RequiredPermission;
import com.fxsen.commons.core.base.result.ResultJson;
import com.fxsen.commons.core.constant.Constants;
import com.fxsen.commons.sysmanager.core.entity.SsoConfig;
import com.fxsen.commons.sysmanager.core.service.ISsoConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * @describe: 单点登录配置控制器
 * @date: 2022/1/27 21:30
 * @author: fxsen
 */
@Controller
@RequestMapping("sso")
@RequiredPermission("sso")
@Api(tags = "系统：系统接入")
public class SsoConfigController {
    @Autowired
    private ISsoConfigService ssoService;
    @GetMapping("list")
    @RequiredPermission("sso:list")
    @ApiOperation(value = "列表")
    public ResultJson list(int page,int limit,String name,Integer status){
        try {
            LambdaQueryWrapper<SsoConfig> query = new LambdaQueryWrapper<>();
            query.eq(null != status,SsoConfig :: getStatus,status);
            query.like(StringUtils.isNotBlank(name),SsoConfig :: getName,name);
            query.orderByAsc(SsoConfig :: getOrderIndex);
            Page<SsoConfig> ssoConfigPage = ssoService.page(new Page<>(page, limit), query);
            return ResultJson.success(ssoConfigPage);
        }catch (Exception e){
            e.printStackTrace();
            return ResultJson.ERROR_ERRO;
        }
    }
    @ApiOperation(value = "新增")
    @PostMapping("save")
    @RecordLog("单点登录-系统接入-新增")
    @RequiredPermission("sso:save")
    public ResultJson save(@RequestBody SsoConfig ssoConfig, HttpServletRequest request, HttpServletResponse response){
        try {
            if (null == ssoConfig){
                return ResultJson.ERROR_PARAM;
            }
            return ssoService.save(ssoConfig) ? ResultJson.success() : ResultJson.ERROR_SAVE;
        }catch (Exception e){
            e.printStackTrace();
            return ResultJson.ERROR_ERRO;
        }
    }
    @ApiOperation(value = "更新")
    @PostMapping("update")
    @RecordLog("单点登录-系统接入-更新")
    @RequiredPermission("sso:update")
    public ResultJson update(@RequestBody SsoConfig ssoConfig, HttpServletRequest request, HttpServletResponse response){
        try {
            if (null == ssoConfig || StringUtils.isBlank(ssoConfig.getId())){
                return ResultJson.ERROR_PARAM;
            }
            return ssoService.updateById(ssoConfig) ? ResultJson.success() : ResultJson.ERROR_SAVE;
        }catch (Exception e){
            e.printStackTrace();
            return ResultJson.ERROR_ERRO;
        }
    }
    @ApiOperation(value = "删除")
    @GetMapping("delete")
    @RecordLog("单点登录-系统接入-删除")
    @RequiredPermission("sso:delete")
    public ResultJson delete(@RequestParam("id") String id, HttpServletRequest request, HttpServletResponse response){
        try {
            return ssoService.removeById(id) ? ResultJson.success() : ResultJson.ERROR_DELETE;
        }catch (Exception e){
            e.printStackTrace();
            return ResultJson.ERROR_ERRO;
        }
    }
    @ApiOperation(value = "状态开关")
    @GetMapping("checkbox")
    @RecordLog("单点登录-系统接入-修改状态")
    @RequiredPermission("sso:checkbox")
    public ResultJson checkbox(@RequestParam("id") String id, HttpServletRequest request, HttpServletResponse response){
        try {
            SsoConfig sso = ssoService.getById(id);
            sso.setStatus(sso.getStatus().equals(Constants.STATUS_NO) ? Constants.STATUS_YES : Constants.STATUS_NO);
            return ssoService.updateById(sso) ? ResultJson.success() : ResultJson.ERROR_UPDATE;
        }catch (Exception e){
            e.printStackTrace();
            return ResultJson.ERROR_ERRO;
        }
    }
}
